﻿/**
 * GoText Palm: A program that allows to send text messages over the internet.
 *  Copyright (C) 2008 Nicola 'Gawaine' Racco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.IO;
using System.Reflection;

namespace GoTextPalm.Core.Persistence
{
    class DB
    {
        public static void CreateDB()
        {
            String path = Program.Path;
            String connStr = "Data Source=" + path + @"\gtpdb.sdf";

            SqlCeEngine eng = new SqlCeEngine(connStr);
            eng.CreateDatabase();
            eng.Dispose();
        }

        public static void UpdateDB()
        {
            String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            String connStr = "Data Source=" + path + @"\gtpdb.sdf";
            SqlCeConnection conn = new SqlCeConnection(connStr);
            conn.Open();

            StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("GoTextPalm.Core.Persistence.updateFromBeta1.sql"));
            String[] cmds = sr.ReadToEnd().Split(';');
            sr.Close();

            SqlCeCommand cmd = conn.CreateCommand();
            foreach (String cmdTxt in cmds)
            {
                cmd.CommandText = cmdTxt;
                cmd.ExecuteNonQuery();
            }
            cmd.Dispose();
            conn.Close();
        }

        public static void InstallDB()
        {
            String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            String connStr = "Data Source=" + path + @"\gtpdb.sdf";
            SqlCeConnection conn = new SqlCeConnection(connStr);
            conn.Open();

            StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("GoTextPalm.Core.Persistence.db.sql"));
            String[] cmds = sr.ReadToEnd().Split(';');
            sr.Close();

            SqlCeCommand cmd = conn.CreateCommand();
            foreach (String cmdTxt in cmds)
            {
                cmd.CommandText = cmdTxt;
                cmd.ExecuteNonQuery();
            }
            cmd.Dispose();
            conn.Close();
        }

        public static Boolean ExistsDB()
        {
            String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            String connStr = "Data Source=" + path + @"\gtpdb.sdf";
            SqlCeConnection conn = new SqlCeConnection(connStr);
            String Database = conn.Database;
            conn.Close();
            return System.IO.File.Exists(Database);
        }

        private DB()
        {
            String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            String connStr = "Data Source=" + path + @"\gtpdb.sdf";
            conn = new SqlCeConnection(connStr);
            conn.Open();
        }

        ~DB()
        {
            conn.Close();
        }

        private static DB instance;
        public static DB Instance
        {
            get
            {
                return instance == null ? (instance = new DB()) : instance;
            }
            private set
            {
                instance = value;
            }
        }

        private SqlCeConnection conn;

        public SqlCeCommand NewCommand()
        {
            return conn.CreateCommand();
        }
    }
}
